package com.xzr.hospitalregister.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.xzr.hospitalregister.R;
import com.xzr.hospitalregister.config.HttpUrl;
import com.xzr.hospitalregister.entity.DialogIpSelectItem;
import com.xzr.hospitalregister.entity.SomethingNeed;


public class IpSetActivity extends AppCompatActivity {
    private TextView nowIp;
    private EditText newIp, newDuanKou;
//    private TextView newSelect;
    private Button certain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_ip_set);
        nowIp = (TextView) findViewById(R.id.tv_acitivity_ip_set_nowIp);
        newIp = (EditText) findViewById(R.id.tv_acitivity_ip_set_newIp);
        newDuanKou = (EditText) findViewById(R.id.tv_acitivity_ip_set_newDuanKou);
        certain = (Button) findViewById(R.id.btn_activity_ip_set_certainNew);
//        newSelect = (TextView) findViewById(R.id.tv_acitivity_ip_set_newSelect);


        SharedPreferences sp = getSharedPreferences("IpSet", MODE_PRIVATE);
        String ipValue = sp.getString("ipValue", null);
        if (ipValue == null) {
            nowIp.setText("当前IP为空！");
            nowIp.setTextColor(getResources().getColor(R.color.red));
        } else {
            nowIp.setText("当前IP：" + ipValue);
            nowIp.setTextColor(getResources().getColor(R.color.black));
        }

//        newSelect.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                DialogIpSelect dialogIpSelect = new DialogIpSelect();
//                dialogIpSelect.show(getSupportFragmentManager(), "DialogIpSelect");
//            }
//        });

        certain.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(newIp.getText().toString()) && !TextUtils.isEmpty(newDuanKou.getText().toString())) {

                    SharedPreferences sp = getSharedPreferences("IpSet", MODE_PRIVATE);
                    SharedPreferences.Editor edit = sp.edit();
                    String value = newIp.getText().toString() + ":" + newDuanKou.getText().toString();
                    edit.putString("ipValue", value);//设置默认ip
                    edit.commit();
                    nowIp.setText("当前IP：" + value);
                    SomethingNeed.getInstance().setIp(value);
                    HttpUrl.initUrl();
                    Toast.makeText(IpSetActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(IpSetActivity.this, "请填写IP地址和端口号！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void setSelectIpToWidget(DialogIpSelectItem dialogIpSelectItem) {
        newIp.setText(dialogIpSelectItem.getCityIp().split(":")[0]);
        newDuanKou.setText(dialogIpSelectItem.getCityIp().split(":")[1]);
    }
}
